package util.validate
{
	import MyApp.Model.Business.Member;
	import MyApp.Model.Business.Staff;
	
	import mx.collections.ArrayCollection;
	import mx.utils.StringUtil;
	import mx.validators.ValidationResult;
	import mx.validators.Validator;
	
	import nz.co.codec.flexorm.EntityManager;
	import nz.co.codec.flexorm.criteria.Criteria;
	
	public class LoginOtherValidator extends Validator
	{
		/**
		 *要验证的数据类型可以的属性有："card","phone","identity","email" 
		 */ 
		public var type:String="";
		[Bindable]
		/**
		 *如果验证成功返回的会员 
		 */ 
		public var member:Member;
		public function LoginOtherValidator()
		{
			super();
		}
		
		override protected function doValidation(value:Object):Array{
			var results:Array=super.doValidation(value);
			if(results.length>0){
				return results;
			}
			//数据库验证
			value=StringUtil.trim(String(value));
			var em:EntityManager=EntityManager.getInstance();
			var cri:Criteria;
			var mem:Member;
			switch(type){
				case "card":{
					cri=em.createCriteria(Member);
					cri.addEqualsCondition("member_no",value);
					mem=em.fetchCriteriaFirstResult(cri) as Member;
					if(mem==null){
						results.push(new ValidationResult(true,"","cardError","卡号错误或不存在"));
					}else{
						member=mem;
					}
				}break;
				case "phone":{
					if(member==null){
						results.push(new ValidationResult(true,"","Error","请先填写卡号"));
						break;
					}
					cri=em.createCriteria(Member);
					cri.addEqualsCondition("mobile",value);
					cri.addEqualsCondition("member_id",member.member_id);
					mem=em.fetchCriteriaFirstResult(cri) as Member;
					if(mem==null){
						results.push(new ValidationResult(true,"","mobileError","手机号错误或不存在"));
					}
				}break;
				case "identity":{
					if(member==null){
						results.push(new ValidationResult(true,"","Error","请先填写卡号"));
						break;
					}
					cri=em.createCriteria(Member);
					cri.addEqualsCondition("identity_no",value);
					cri.addEqualsCondition("member_id",member.member_id);
					mem=em.fetchCriteriaFirstResult(cri) as Member;
					if(mem==null){
						results.push(new ValidationResult(true,"","identityError","身份证号错误或不存在"));
					}
				}break;
				case "email":{
					if(member==null){
						results.push(new ValidationResult(true,"","Error","请先填写卡号"));
						break;
					}
					cri=em.createCriteria(Member);
					cri.addEqualsCondition("email",value);
					cri.addEqualsCondition("member_id",member.member_id);
					mem=em.fetchCriteriaFirstResult(cri) as Member;
					if(mem==null){
						results.push(new ValidationResult(true,"","emailError","Email错误或不存在"));
					}
				}
			}
			return results;
		}
	}
}